// pages/instituadd/instituadd.js
import loginFn from '../../utils/login'
import urls from '../../utils/urls'
//获取应用实例
const app = getApp()
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		list: [], //机构列表
		keyword: '',
		page: 1,
		totalPage: null
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function(options) {
		console.log('options:',options);
		this.getlist(options)
	},
	getlist: function(options) { //获取机构列表
		var that = this;

		var allschool = loginFn();
		allschool.keyword = this.data.keyword;
		let page = this.data.page;
		let totalPage = this.data.totalPage;

		if(totalPage !== null && page > totalPage) {
			return false;
		}

		allschool.page = page;

		wx.request({
			url: urls.allschool,
			data: allschool,
			success: function(res) {
				if (res.data.code == '0') {
					page ++;
					let list = that.data.list.concat(res.data.data.list);
					that.setData({
						list: list,
						totalPage: res.data.data.totalPage,
						page: page
					})
				};
			}
		})
	},
	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function() {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function() {
		// var loginInfo = loginFn();


		// this.getlist(options)
	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function() {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function() {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function() {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function() {
		this.getlist();
	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function() {

	},
	//确定加入
	join(e) {
		console.log(e)
		var that = this;
		wx.showModal({
			title: '提示',
			content: '确定加入该机构吗?',
			success: function(r) {
				if (r.confirm) {
					var postData = loginFn();
					postData.schoolId = e.currentTarget.dataset.id;
					postData.status = 0;
					wx.request({
						url: urls.saveDel,
						method: 'GET',
						data: postData,
						success: function(res) {
							if (res.data.code == '0') {
								wx.showToast({
									title: '提交成功'
								})
							}
						}
					})
				}
			}
		})
	},
	//输入搜索关键字
	inputKeyword(e) {
		this.setData({
			keyword: e.detail.value
		})
	},
	//搜索
	search() {
		this.setData({
			list: [],
			totalPage: null,
			page: 1
		});

		this.getlist();
	}
})
